package com.myl.web.controller.system;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.myl.domain.system.Dept;
import com.myl.domain.system.Module;
import com.myl.domain.system.Role;
import com.myl.domain.system.User;
import com.myl.service.system.UserService;
import com.myl.service.system.impl.UserServiceImp;
import com.myl.utils.BeanUtil;
import com.myl.utils.MD5Util;
import com.myl.web.controller.BaseServlet;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/system/user")
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String operation = request.getParameter("operation");
		if("list".equals(operation)){
			
			this.list(request,response);
			
		}else if ("toAdd".equals(operation)) {
			this.toAdd(request, response);
		}else if ("save".equals(operation)) {
			this.save(request, response);
		}else if ("toEdit".equals(operation)) {
	         this.toEdit(request,response);
        }else if ("edit".equals(operation)) {
        	this.edit(request,response);
		}else if ("delete".equals(operation)) {
        	this.delete(request,response);
		}else if ("userRoleList".equals(operation)) {
        	this.userRoleList(request,response);
		}else if ("updateRole".equals(operation)) {
        	this.updateRole(request,response);
		}else if ("login".equals(operation)) {
        	this.login(request,response);
		}else if ("home".equals(operation)) {
        	this.home(request,response);
		}else if ("logout".equals(operation)) {
        	this.logout(request,response);
		}
	
	}

	

	private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		request.getSession().removeAttribute("loginUser");
		response.sendRedirect(request.getContextPath()+"/login.jsp");
	}

	private void home(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.getRequestDispatcher("/WEB-INF/pages/home/home.jsp").forward(request, response);
		
	}

	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		/*System.out.println(email);
		System.out.println(password);*/
	    User user = userService.login(email,password);
	    if(user!=null){
	    	 request.getSession().setAttribute("loginUser", user);
	 	    //跳转到主页面
	    	 List<Module> moduleList = userService.findModuleById(user.getId());
	    	 request.setAttribute("moduleList", moduleList);    	
	 	    request.getRequestDispatcher("/WEB-INF/pages/home/main.jsp").forward(request, response);
	    }else {
			response.sendRedirect(request.getContextPath()+"/login.jsp");
		}
	   
		
	}

	private void updateRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String userId = request.getParameter("userId");
		String[] roleIds = request.getParameterValues("roleIds");
		System.out.println(userId);
		System.out.println(roleIds.length);
		userService.updateRole(userId,roleIds);
		list(request, response);
		
		
	}

	private void userRoleList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String UserId = request.getParameter("id");
		User user = userService.findById(UserId);
		request.setAttribute("user", user);
		
		List<Role> all = roleService.findAllRoleByUserId(UserId);
		request.setAttribute("roleList", all);
		request.getRequestDispatcher("/WEB-INF/pages/system/user/role.jsp").forward(request, response);
		
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		User user = BeanUtil.fillBean(request, User.class,"yyyy-MM-dd");
		UserService userService = new UserServiceImp();
		userService.delete(user);
		list(request, response);
		
	}

	private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = BeanUtil.fillBean(request, User.class,"yyyy-MM-dd");
		
		UserService userService = new UserServiceImp();
		userService.update(user);
				
		list(request, response);
	}

	private void toEdit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		String id = request.getParameter("id");
		UserService userService = new UserServiceImp();
		User user = userService.findById(id);
		request.setAttribute("user", user);
		List<Dept> all = deptService.findAll();
		request.setAttribute("deptList", all);
		request.getRequestDispatcher("/WEB-INF/pages/system/user/update.jsp").forward(request, response);
		//response.sendRedirect(request.getContextPath()+"/system/user?operation=list");
		
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//进入列表页，查询全部数据，存入session，调用显示
		UserService userService = new UserServiceImp();
		int page =1;
		int size=5;
	   if((request.getParameterValues("page"))!=null){
		   page = Integer.parseInt(request.getParameter("page"));
	   }
	   if((request.getParameterValues("size"))!=null){
		   size = Integer.parseInt(request.getParameter("size"));
	   }
		
		
		PageInfo all = userService.findAll(page, size);
		
		request.setAttribute("page", all);
		
		request.getRequestDispatcher("/WEB-INF/pages/system/user/list.jsp").forward(request, response);
		
	}

	private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<User> all = userService.findAll();
		request.setAttribute("userList", all);
		
		request.getRequestDispatcher("/WEB-INF/pages/system/user/add.jsp").forward(request, response);
		
	}
     private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		User user = BeanUtil.fillBean(request, User.class,"yyyy-MM-dd");
		UserService userService = new UserServiceImp();
		userService.save(user);
		//response.sendRedirect(request.getContextPath()+"/system/user?operation=list");
		list(request, response);
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
